 ## library(stringr)

setwd("../../data/voter_file_aggregated")

CIs <- TRUE

## use plot_partychanges_by_move_distance_raw.R for other years
file <- c("12to14")

## pdf(paste("moving_changing_party_12to14_simple_noagecontrol_controlpastmovers",ifelse(CIs,"_withcis",""),".pdf",sep=""),width=5,height=5)
par(mar=c(5,6,2,1),mfrow=c(1,1))

the.col <- "purple3"

load(paste("moves_distance_",file,"_simple_noagecontrol.RData",sep=""))

the.means <- exp(the.means)-1           #this will be the x axis
the.intercept <- the.coefs[1]
the.coefs <- the.coefs[grep("cut",names(the.coefs))] + the.intercept
distances <- the.means[-1]                                           #x axis


the.coefs <- data.frame(
    distance=distances,
    est=unname(the.coefs)
    )

the.preds.upper <- 1- (the.preds$fit - qt(0.975,the.preds$df)*the.preds$se)
the.preds.lower <- 1- (the.preds$fit + qt(0.975,the.preds$df)*the.preds$se)
## the.preds used controls. can also run 1 - the.coefs$est

if (file == "12to14") {

with(
    the.coefs,
    plot(
        log(the.means+1), 1-the.preds$fit,
        pch=16,cex=1.2,
        xlim=c(0.75, 4.3),
        ylim=c(0,0.05),
        cex.lab=1.3,
        bty="n",
        xlab="",
         xaxt="n",
        ylab="Party-switching rate\n", col=the.col
        )
    )
mtext("Miles from previous residence",side=1,padj=3,cex=1.3)
mtext("(movers only, new zip code)",side=2,padj=-5)
mtext("(in same county, mean in quantile)",side=1,padj=6) #, ^2
axis(1, at = 0:5, labels=sprintf("%.1f", round(c(0, (exp(1:5)-1)*0.62), 1)))

} else {
with(
    the.coefs,
    points(
        log(distance+1), est,
        pch=16, cex=1.2, col=the.col
        )
    )
}

with(
    the.coefs,
    lines(
        loess.smooth(log(the.means+1), 1-c(the.preds$fit),span=1,degree=2, family="symmetric"),
        lwd=4, col=the.col
        )
    )

if (CIs) {
for (i in 1:length(the.preds.lower)){
    segments(
        x0=log(the.means[i]+1),
        x1=log(the.means[i]+1),
        y0=the.preds.lower[i],
        y1=the.preds.upper[i],
        col=the.col
        )
}
}
abline(h = 0, lty = 2, col = "red")

if (CIs) {
text(1.7,0.003,labels="Predominantly\nurban moves",cex=1)
}
mtext(side=3,c("CALIFORNIA, 2012-2014\n(open primaries)"),at=log(12),cex=1,padj=1)



the.col <- "gray"

load(paste("moves_distance_",file,"_simple_noagecontrol_controlpastmoves_pastmoversonly.RData",sep=""))

the.means <- exp(the.means)-1
the.intercept <- the.coefs[1]
the.coefs <- the.coefs[grep("cut",names(the.coefs))] + the.intercept + the.coefs["addMovers"]
distances <- cbind(
    the.means[-1]
    )

the.coefs <- data.frame(
    distance=distances,
    est=unname(the.coefs)
    )

the.preds.upper <- 1- (the.preds$fit - qt(0.975,the.preds$df)*the.preds$se)
the.preds.lower <- 1- (the.preds$fit + qt(0.975,the.preds$df)*the.preds$se)


if (file == "12to14") {

with(
    the.coefs,
    points(
        log(the.means+1), 1-the.preds$fit,
        pch=16,cex=1.2,
        xlim=c(0.75, 4.3),
        ylim=c(0,0.05),
        cex.lab=1.3,
        bty="n",
        xlab="",
         xaxt="n",
        ylab="Change party affiliation\n", col=the.col
        )
    )
mtext("Miles from previous residence",side=1,padj=3,cex=1.3)
mtext("(movers only, new zip code)",side=2,padj=-5)
mtext("(in same county, mean in quantile)",side=1,padj=6) #, ^2
axis(1, at = 0:5, labels=sprintf("%.1f", round(c(0, (exp(1:5)-1)*0.62), 1)))

} else {

with(
    the.coefs,
    points(
        log(distance+1), est,
        pch=16, cex=1.2, col=the.col
        )
    )
}


if (CIs) {
for (i in 1:length(the.preds.lower)){
    segments(
        x0=log(the.means[i]+1),
        x1=log(the.means[i]+1),
        y0=the.preds.lower[i],
        y1=the.preds.upper[i],
        col=the.col
        )
}
}

with(
    the.coefs,
    lines(
        loess.smooth(log(the.means+1), 1-the.preds$fit,span=1,degree=2, family="symmetric"),
        lwd=4, col=the.col
        )
    )

legend(x=log(3),y=0.045,legend=c("All movers","Repeat movers\n(with past move controls)"),pch=16,col=c("purple3","gray"),bty="n")

if (!CIs) {
lines(x=log(3:60), y=rep(0.005, 58), lwd = 3, lty = 2)
text(x=log(15),y=0.008, labels="partisan attachment")
}
## dev.off()
